# Copyright (c) 2022, hzmes and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from srm.srm.doctype.srm_server_config.srm_server_config import sync_to_k3cloud_price_adjustment, validate_before_price_adjustmen

class SRMPriceCompare(Document):
	# 调用同步生成ERP调价单方法
	@frappe.whitelist()
	def sync_to_price_adjustment(self, session_fullname):
		# 2023-10-23: 增加对当前采购员是否已经同步了云星空的userid
		userid = frappe.get_doc('User', frappe.session.user).mobile_no
		if userid:
			# 定义一个空的接收列表变量
			fbillno_r = []
			# 循环子表记录
			for i in self.material_subs:
				# 只有"选中"的记录行才真正调用同步函数
				if i.selected == 1:
					# 2023-04-11: 在sync_to_k3cloud_price_adjustment前先校验, 确保供应商和采购价目表可用
					validate = validate_before_price_adjustmen(i.supplier_number)
					if validate:
						r = sync_to_k3cloud_price_adjustment(supplier_number=i.supplier_number, material_number=i.material_number, price=i.price, taxprice=i.tax_price, taxrate=i.tax_rate, docname=self.name, session_fullname=session_fullname, currency=self.currency, creatorid=userid)
						if r:
							fbillno_r.append(r)
			# 往JS返回
			return fbillno_r
		else:
			frappe.msgprint('当前用户没有同步云星空的UserID!')
					

